package com.shop.servlet;

import com.shop.model.CartItem;
import com.shop.model.Course;
import com.shop.service.BuyService;
import com.shop.service.CourseService;
import com.shop.service.IBuyService;
import com.shop.service.ICourseService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/buy")
public class BuyServlet extends HttpServlet {


    IBuyService ibuyService =new BuyService();
    ICourseService icourseService = new CourseService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        req.setCharacterEncoding("UTF-8");

        //处理不同的请求
        String source = req.getParameter("source");
        if(source == null){
            throw new ServletException("缺少来源参数");
        }

        String studentId = req.getParameter("studentId");
        if (studentId == null || studentId.isEmpty()) {
            throw new ServletException("学生ID不能为空");
        }



        if("page1".equals(source)) {//page1是直接购买分流界面
            int courseId = Integer.parseInt(req.getParameter("courseId"));
            List<Course> course = icourseService.searchCourse(courseId, null);
            for (Course c : course) {
                System.out.println("BuyServlet:29:" + c.getCourseName() + " " + c.getPrice() + " " + c.getCourseId());//调试
            }
            double buy_tprice = ibuyService.buy_tprice(course, studentId);
            List<Double> buy_price = ibuyService.buy_price(course, studentId);

            req.setAttribute("buy_tprice", buy_tprice);
            req.setAttribute("course", course);
            req.setAttribute("studentId",studentId);
            req.setAttribute("buy_price",buy_price);
            req.getRequestDispatcher("order.jsp").forward(req, resp);
        } else if ("page2".equals(source)) {//page2是购物车分流界面
            HttpSession session = req.getSession();
            List<CartItem> cartItems = (List<CartItem>) session.getAttribute("cart");
            if (cartItems == null || cartItems.isEmpty()) {
                throw new ServletException("购物车为空");
            }
            List<Course> courses = new ArrayList<>();
            for(CartItem cartItem : cartItems) {
                Course course = new Course(cartItem.getCourse().getCourseName(),
                        cartItem.getCourse().getCourseId(),
                        cartItem.getCourse().getTeacher(),
                        cartItem.getCourse().getPrice(),
                        cartItem.getCourse().getStartDate(),
                        cartItem.getCourse().getEndDate(),
                        cartItem.getCourse().getType(),
                        cartItem.getCourse().getCapacity(),
                        cartItem.getCourse().getTime());

                    courses.add(course);

            }
            for(Course c : courses) {
                System.out.println("BuyServlet:58:" + c.getCourseName() + " " + c.getPrice() + " " + c.getCourseId());//调试
            }
            double buy_tprice = 0;
            List<Double> buy_price = new ArrayList<>();
            buy_tprice = ibuyService.buy_tprice(courses, studentId);
            buy_price = ibuyService.buy_price(courses, studentId);

            req.setAttribute("buy_tprice", buy_tprice);
            req.setAttribute("course", courses);
            req.setAttribute("studentId",studentId);
            req.setAttribute("buy_price",buy_price);
            req.getRequestDispatcher("order.jsp").forward(req, resp);
        }
    }

}
